nsbase.ui.utility = new function () {
    /// <field name="htmlEncode" type="String">Encode html string</field>
    /// <field name="htmlDecode" type="String">Decode html string</field>
    var _self = this;

    function main() {
        _self.htmlEncode = htmlEncode;
        _self.htmlDecode = htmlDecode;
        _self.exportData = exportData;
        _self.getAccountImage = getAccountImage;
        _self.openBlankWindow = openBlankWindow;
        _self.getRegion = getRegion;// dependency config.region
    //_self.appendAntiForgeryToken = appendAntiForgeryToken;

    }

    function htmlEncode(str) {
        return str.replace(/&/g, '&amp;').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    };
    function htmlDecode(str) {
        return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"');
    };

    function exportData(frameContainer, actionUrl, actionParam) {
        var exportForm = $('<form method="post" action="' + actionUrl + '" style="display:none"></form>'),
        exportBody = frameContainer.children("iframe").contents().find("body");

        exportBody.append(exportForm);
        for (var key in actionParam) {
            var input = $('<input id="' + key + '" type="hidden" value="" name="' + key + '">');
            input.val(actionParam[key]);
            exportForm.append(input);
        }
        exportForm.submit();
        exportForm.remove();
    }
    function openBlankWindow(actionUrl,actionParam) {
        var blankForm=$('<form method="post" action="' + actionUrl + '" target="_blank" style="display:none"></form>');
        $("body").append(blankForm);
        
        for (var key in actionParam) {
            var input = $('<input id="' + key + '" type="hidden" value="" name="' + key + '">');
            input.val(actionParam[key]);
            blankForm.append(input);
        }
        blankForm.submit();
        blankForm.remove();
    }
    function getAccountImage(imageUrl) {
        var reg = new RegExp("^http(|s)://");
        if (reg.test(imageUrl)) {
            return imageUrl;
        }
        var defaultImage = "../images/Account/defaultProfilePicture.gif";
        return defaultImage;
    }

    function getRegion(provCode,cityCode){
        provCode = parseInt(provCode) === 0? "100": provCode;
        cityCode = parseInt(cityCode) === 0? "100": cityCode;
        var region = {
            provCode: provCode,
            provName:"",
            cityCode:cityCode,
            cityName:""
        };

        var regions = nsbase.config.region;
        var provCodes = regions.provcodes.split(","),
            provNames = regions.provinces.split(",");

        for (var i = 0;i<provCodes.length;i++) {
            if(provCodes[i] == provCode){
               region.provName = provNames[i];
                break;
            }
        };

        var cityCodes = regions["code"+ provCode].split(","),
            cityNames = regions["prov"+ provCode].split(",");
        for (var i = 0; i < cityCodes.length; i++) {
            if(cityCodes[i] === cityCode){
                region.cityName = cityNames[i];
                break;
            }
        };
        return region;
    }
   
    main();
}();